<?php
namespace Home\Controller;
use Think\Controller;
class StudentlistController extends BackController {
	public function index(){
		$user = M('user')->select();
		$this->assign('sc_user',$user);
		
		$sc_student = M('sc_student');
		$pageid = I('get.pageid',1,'int');//接收当前页\
		$where="";
		if(session('qe_user')['xid']!=0){
			$where['xid'] = array('eq',session('qe_user')['xid'],'and');
		}		
		$where['state_id'] = array('eq',13,'and');
		$where['up'] = array('eq',0,'and');
		$date = I('get.date','','addslashes');
		if($date != ''){
			$where['news_time'] = array('eq',$date,'and');
			$this->assign('date',$date);
		}
		$count = $sc_student->where($where)->count();//计算出总的记录数
		$page_news = 20;//每页显示1条数据
		$page['nowpage'] = $pageid;//当前页
		$page['prev'] = $pageid-1;//上一页
		$page['next'] = $pageid+1;//下一页
		$page['count'] = ceil($count/$page_news);//总页数
		if($page['nowpage']<=1){
			$page['prev'] = 1;
			$page['nowpage'] = 1;
		};
		if($page['next']>=$page['count']){
			$page['next'] = $page['count'];
		};
		$sc_data = $sc_student->where($where)->order('edit_time desc,id desc')->page($pageid.','.$page_news)->select();
		foreach($sc_data as $k=>$v){
			$sc_data[$k]['record_data'] = $this->record_data($v['id']);
			$sc_data[$k]['xs_name'] = $this->username($v['xsid']);
			$sc_data[$k]['am_name'] = $this->basname($v['am_id']);
			$sc_data[$k]['state_name'] = $this->basname($v['state_id']);
			$sc_data[$k]['x_name'] = $this->basname($v['xid']);
		}
		$this->assign('sc_data',$sc_data);
		$this->assign('page',$page);
		
		$campus = M('other')->select();
		$this->assign('campus',$campus);
		//相关课程
		//var_dump($sc_data);
		$this->display();
	}
	public function edit(){
		if(IS_POST) {
			$student = D('Student');
			$id = I('post.id','','int');
			if(M('contract')->where('student='.$id)->find()){
				$_POST['up'] = 1;
				$_POST['edit_time'] = date("Y-m-d");
				if($student->create()) {
						if($student->where('id='.$id)->save($_POST) !== false) {
								$this->success('报名成功',U('Studentlist/index'));
						}else {
								$this->error('报名失败',U('Studentlist/index'));
						}
				}else {
						$this->error($record->getError());
				}
			}else{
				$this->error('请先添加报名合同',U('Contract/index'));
			}
		}
	}
}